package com.hyjx.framework.component.mutidatasource.datasources;

import com.hyjx.framework.common.CommonServlet;
import com.hyjx.framework.common.CommonUtil;
import com.hyjx.framework.common.util.StringHelper;
import com.hyjx.framework.orm.dao.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 多数据源的controller
 */
@Controller
@RequestMapping("/datasource")
public class DataSourcesController {
    @Resource
    private DataSourcesService service;

    /**
     * 到多数据源列表页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoDsList", method = RequestMethod.GET)
    public String gotoList(HttpServletRequest request) {
        return "/framework/datasources/datasources_list";
    }


    /**
     * 查询列表数据
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/queryDsList", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String queryList(HttpServletRequest request, DataSourcesModel model) {
        Pager li = service.queryListA(model);
        return CommonUtil.getPageJsonStr(li);
    }

    /**
     * 到新增页面
     *
     * @return
     */
    @RequestMapping(value = "/gotoDsAdd", method = {RequestMethod.GET, RequestMethod.POST})
    public String gotoAdd(HttpServletRequest request) {
        return "/framework/datasources/datasources_add";
    }


    /**
     * 到修改页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoEdit", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView gotoEdit(HttpServletRequest request) {
        String id = request.getParameter("id");

        DataSourcesModel qp = new DataSourcesModel();

        qp.setId(id);
        DataSourcesModel model = (DataSourcesModel) (service.getInfoById(qp));

        ModelAndView mav = new ModelAndView("/framework/datasources/datasources_edit");
        mav.addObject("model", model);
        return mav;
    }


    /**
     * 删除
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoDelete", method = RequestMethod.GET)
    @ResponseBody
    public String gotoDelete(HttpServletRequest request) {
        String id = request.getParameter("ids");

        DataSourcesModel qp = new DataSourcesModel();
        qp.setId(id);

        if (service.delete(qp) > 0) {
            CommonServlet.setMutiDataSource();
            return "success";
        } else {
            return "failure";
        }
    }


    /**
     * 到查看页面
     * request
     *
     * @return
     */
    @RequestMapping(value = "/gotoView", method = RequestMethod.GET)
    public ModelAndView gotoView(HttpServletRequest request) {
        String id = request.getParameter("id");
        DataSourcesModel qp = new DataSourcesModel();
        qp.setId(id);
        DataSourcesModel model = (DataSourcesModel) (service.getInfoById(qp));

        ModelAndView mav = new ModelAndView("/framework/datasources/datasources_view");
        mav.addObject("model", model);
        return mav;
    }

    /**
     * 保存对象
     *
     * @param DataUserModel
     * @return
     */
    @RequestMapping(value = "/doAdd", method = RequestMethod.POST)
    @ResponseBody
    public String doAdd(DataSourcesModel model) {
        model.setId(StringHelper.getPrimaryKey());
        if (service.add(model)) {
            CommonServlet.setMutiDataSource();
            return "success";
        } else {
            return "failure";
        }
    }

    /**
     * 更新数据
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/doEdit", method = RequestMethod.POST)
    @ResponseBody
    public String doEdit(DataSourcesModel model) {
        if (service.edit(model) > 0) {
            CommonServlet.setMutiDataSource();
            return "success";
        } else {
            return "failure";
        }
    }

}
